<?php
/**
 * 区间模型
 */

namespace app\official\model;


use app\common\model\BaseModel;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;

class IntervalConfig extends BaseModel {

    const CONFIG_TYPE1 = 1;//价格
    const CONFIG_TYPE2 = 2;//面积
    const CONFIG_TYPE3 = 3;//折扣
    const CONFIG_TYPE4 = 4;//楼龄
    const CONFIG_TYPE5 = 5;//楼层

    //排序
    public static $orderby = [
        ['key' => 1, 'name' => '默认排序'],
        ['key' => 2, 'name' => '最新发布'],
        ['key' => 3, 'name' => '价格高低'],
        ['key' => 4, 'name' => '价格低高'],
        ['key' => 5, 'name' => '折扣高低'],
        ['key' => 6, 'name' => '折扣低高'],
        ['key' => 7, 'name' => '开拍时间远近'],
        ['key' => 8, 'name' => '开拍时间近远'],
    ];

    //拍卖阶段
    public static $a_type = [
        ['key' => 1, 'name' => '一拍'],
        ['key' => 2, 'name' => '二拍'],
        //        ['key' => 3, 'name' => '三拍'],
        ['key' => 4, 'name' => '变卖'],
        ['key' => 5, 'name' => '重新拍卖'],
        //        ['key' => 6, 'name' => '破产'],
    ];

    //拍卖状态
    public static $a_status_ = [
        ['key' => 1, 'name' => '待开始'],
        ['key' => 2, 'name' => '进行中'],
        ['key' => 3, 'name' => '已成交'],
        ['key' => 4, 'name' => '已流拍'],
        ['key' => 5, 'name' => '已撤回'],
        ['key' => 6, 'name' => '已终止'],
        ['key' => 7, 'name' => '已暂缓'],
    ];

    //户型
    public static $bedroom = [
        ['key' => 1, 'name' => '一居室'],
        ['key' => 2, 'name' => '二居室'],
        ['key' => 3, 'name' => '三居室'],
        ['key' => 4, 'name' => '四居室'],
        ['key' => 5, 'name' => '五居室以上'],
    ];

    /**
     * 区间数据
     * @param $city_code
     * @return mixed
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function allIntervel($city_code) {
        $interval_config = Db::name('interval_config')
            ->order('min', 'asc')
            ->whereOr('city_code', 0)
            ->whereOr('city_code', $city_code)
            ->field('type,name,min,max')
            ->select();
        $i = 0;
        do {
            if ($interval_config[$i]['type'] == self::CONFIG_TYPE1) {
                $all_intervel['price_interval'][] = self::deal($interval_config[$i]);
            }
            if ($interval_config[$i]['type'] == self::CONFIG_TYPE2) {
                $all_intervel['build_area_interval'][] = self::deal($interval_config[$i]);
            }
            if ($interval_config[$i]['type'] == self::CONFIG_TYPE3) {
                $all_intervel['discount_interval'][] = self::deal($interval_config[$i]);
            }
            if ($interval_config[$i]['type'] == self::CONFIG_TYPE4) {
                $all_intervel['house_age'][] = self::deal($interval_config[$i]);
            }
            if ($interval_config[$i]['type'] == self::CONFIG_TYPE5) {
                $all_intervel['house_floor'][] = self::deal($interval_config[$i]);
            }
            $i++;
        } while ($i < count($interval_config));
        return $all_intervel;
    }

    /**
     * 区间处理
     * @param $arr
     * @return mixed
     */
    public static function deal($arr) {
        $arr['key'] = $arr['min'] . '-' . $arr['max'];
        unset($arr['min']);
        unset($arr['max']);
        unset($arr['type']);
        return $arr;
    }

}
